/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class VectorBindFieldInterface {
    private Vector store = new Vector();
    
    public VectorBindFieldInterface() {}
    
    public VectorBindFieldInterface(Vector vector) {
        for (int i = 0;i < vector.size();i++) {
            add((BindFieldInterface) vector.get(i));
        }
    }
    
    public VectorBindFieldInterface(BindFieldInterface p_fields[]) {
        for (int i = 0;i < p_fields.length;i++) {
            add(p_fields[i]);
        }
    }
    
    public BindFieldInterface[] toArray() {
        BindFieldInterface fields[] = new BindFieldInterface[size()];
        for (int i = 0;i < size();i++) {
            fields[i] = get(i);
        }
        return fields;
    }
    
    public BindFieldInterface get(int pos) {
        return (BindFieldInterface)store.get(pos);
    }
    
    public BindFieldInterface get(String name) {
        for (int i = 0;i < size();i++) {
            if (get(i).getName().equals(name)) {
                return get(i);
            }
        }
        return null;
    }
    
    public boolean contains(BindFieldInterface item){
        return store.contains(item);
    }
    
    public void add(BindFieldInterface item) {
        store.add(item);
    }
    
    public void add(VectorBindFieldInterface items) {
        for (int i = 0;i < items.size();i++) {
            add(items.get(i));
        }
    }
    
    public void set(int i,BindFieldInterface item)
    throws BindException {
        try {
            store.set(i,item);
        } catch (Exception exception) {
            throw new BindException(exception.getMessage());
        }
    }
    
    public boolean remove(BindFieldInterface item) {
        return store.remove(item);
    }
    
    public BindFieldInterface remove(int pos) {
        return (BindFieldInterface) store.remove(pos);
    }
    
    public int size() {
        return store.size();
    }
    
    public void removeAll(){
        store.removeAllElements();
    }
    
    public List<String> getNames() {
        List<String> vs = new ArrayList<String>();
        for (int i = 0;i < size();i++) {
            vs.add(get(i).getName());
        }
        return vs;
    }
    
    public void setVisibleByDefault(boolean visible) {
        for (int i = 0;i < size();i++) {
            get(i).setVisibleByDefault(visible);
        }
    }
    
    public void rebindFieldsToBindable(Bindable bindable)
    throws BindException {
        for (int i = 0;i < size();i++) {
            boolean set = false;
            for (int e = 0;e < bindable.getBindFields().length && !set;e++) {
                if (get(i).getName().equals(bindable.getBindFields()[e].getName())) {
                    set = true;
                    set(i,bindable.getBindFields()[e]);
                }
            }
            if (!set) {
                throw new BindException("Could not rebind field ["+get(i).getName()+"] from " + bindable);
            }
        }
    }
}

